package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import persistence.ConsultaEditoraDao;
import persistence.ConsultaEditoraDaoImpl;

import model.Editora;

public class BotaoConsultaEditoraController implements ActionListener {

	private DefaultTableModel modelo;
	JTextField txtpesquisa;
	JButton btnOk;

	public BotaoConsultaEditoraController(DefaultTableModel modelo,
			JButton btnOk, JTextField txtpesquisa) {
		this.btnOk = btnOk;
		this.txtpesquisa = txtpesquisa;
		this.modelo = modelo;
	}

	public void actionPerformed(ActionEvent arg0) {
		Editora editora = new Editora();

		if (btnOk.hasFocus() == true) {
			preencheTabela();
		}

	}

	private void preencheTabela() {
		Editora editora = new Editora();
		ConsultaEditoraDao Cedao = new ConsultaEditoraDaoImpl();

		List<Editora> listaeditora = new ArrayList<Editora>();
		try {
			listaeditora = Cedao.listaEditora(editora);
			if (modelo.getRowCount() > 0){
				modelo.setRowCount(0);
			}
			for (Editora ed : listaeditora) {
				Object[] o = new Object[7];
				o[0] = ed.getIdEditora();
				o[1] = ed.getNomeEditora();
				o[2] = ed.getTelefone();

				modelo.addRow(o);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "ERRO",
					JOptionPane.ERROR_MESSAGE);
		}

	}
}
